<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Exceptions\NotLoginException;
use App\Http\Requests\BasicRequest;
use App\Models\ApiBehavior;
use App\Models\ClientUserTicket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CommonController extends Controller
{
	protected $user = [];
	protected $uid;
	
	public function __construct(){
		$request = request();
		$add = [
			'ticket'	=> $request->input('ticket'),
			'cmd'		=> $request->input('_cmd'),
			'ip'		=> $request->ip(),
			'client_system'	=> $request->input('ClientSystem'),
			'client_version'=> $request->input('ClientVersion'),
			'api_version'	=> $request->input('ApiVersion'),
		];
		clear_null($add);
		ApiBehavior::create($add);
	}
	
	public function check_login(){
		$clientUserTicket = new ClientUserTicket();
		$user = $clientUserTicket->checkLogin();

		if($user->type != 1 && $user->type != 3){
			throw new ApiException('用户角色错误，拒绝登录');
		}
		$this->user = $user;
		$this->uid = $user['id'];
	}
	
	/**
	 * @param Request $request
	 * @return array
	 * 省市区下拉框数据筛选
	 */
	public function area_select(Request $request){
		$parent_id = $request->input('parent_id',0);
		$data = DB::table('area')->select(DB::raw('id,name,merger_name,lat,lng'))->where(['parent_id'=>$parent_id])->get();
		return json_success('OK',$data);
	}
}
